<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction(){
    	//header("Set-Cookie: test=axis; domain=.4139.co.cc; path=/");
    }
    
    public function loginAction(){
		$codeSession = new Zend_Session_Namespace('authCode');
    	if($this->getRequest()->isPost()){
    		$validators['username'] = array(new Zend_Validate_NotEmpty()
    									   ,new Zend_Validate_Regex(array('pattern' => '/^[a-zA-Z]{1}[a-zA-Z0-9_]{5,19}$/'))
    									   ,'messages'=>array(0=>'用户名必须填写.',1=>'用户名格式不正确'));
    		$validators['password'] = array(new Zend_Validate_NotEmpty()
   										   ,new Zend_Validate_StringLength(array('min' => 6, 'max' => 25, 'encoding' => 'UTF-8'))
   										   ,'messages'=>array(array(Zend_Validate_NotEmpty::IS_EMPTY => '密码必须填写'),array(Zend_Validate_StringLength::TOO_SHORT => '密码太短')));
    		$validators['authCode'] = array(new Zend_Validate_NotEmpty()
   										   ,'messages'=>'验证码必须填写');
   										   
   			$data = $this->getRequest()->getPost();
			$input = new Zend_Filter_Input(null, $validators, $data);
    		if ($input->hasInvalid()){
    			$codeSession->__unset('code');
				$this->_helper->viewRenderer->setNoRender();
				var_dump($input->getMessages());
				return ;
			}
    		if($codeSession->code != $_POST['authCode']){
    			$codeSession->__unset('code');
    			$this->_helper->viewRenderer->setNoRender();
				echo '验证码错误';
				return ;
    		}
    		
    		$authAdapter = new Zend_Auth_Adapter_DbTable();
    		
    		$authAdapter->setTableName('basicInfo');
            $authAdapter->setIdentityColumn('username');
            $authAdapter->setCredentialColumn('password');
            
            $authAdapter->setIdentity($data['username']);
            $authAdapter->setCredential(md5($data['password']));
                
            $result = $authAdapter->authenticate();
            
            if (!$result->isValid()){
            	$this->_helper->viewRenderer->setNoRender();
				echo '登陆失败';
				return ;
            }
            
    		$data = $authAdapter->getResultRowObject();
            Zend_Auth::getInstance()->setStorage(new Zend_Session_Namespace('auth'))->write($data);
    		$this->_redirect('member');//跳转到会员页面
            
    		/*$userModel = new Application_Model_UserMember();
    		unset($data['authCode']);
    		if($userModel->memberLogin($data)){}*/
    	}
		$captcha = new Zend_Captcha_Image(array('font'=>'./Rival.ttf',
		              'fontsize'=>24,
		              'imgdir'=>APPLICATION_PATH . './data/images/captcha',
		              'session'=>$codeSession,
		              'width'=>200,
		              'height'=>50,
		              'wordlen'=>5 ));
		$captcha->setImgUrl($this->view->baseUrl() . './../application/data/images/captcha');
		$captcha->generate();
		$codeSession->code = $captcha->getWord();
		$this->view->image = $captcha->render();
    }
    
    public function regAction(){
    	if($this->getRequest()->isPost()){
    		/**
    		 * 设置数据验证
    		 */
    		$validators['username'] = array(new Zend_Validate_NotEmpty()
    									   ,new Zend_Validate_Regex(array('pattern' => '/^[a-zA-Z]{1}[a-zA-Z0-9_]{5,19}$/'))
    									   ,'messages'=>array(0=>'用户名必须填写.',1=>'用户名格式不正确'));
   			$validators['password'] = array(new Zend_Validate_NotEmpty()
   										   ,new Zend_Validate_StringLength(array('min' => 6, 'max' => 25, 'encoding' => 'UTF-8'))
   										   ,'messages'=>array(array(Zend_Validate_NotEmpty::IS_EMPTY => '密码必须填写'),array(Zend_Validate_StringLength::TOO_SHORT => '密码太短')));
   			$validators['confirmPassword'] = array(new Zend_Validate_NotEmpty()
   										   ,'messages'=>'密码确认必须填写');
   			$validators['realName'] = array(new Zend_Validate_NotEmpty()
   										   ,'messages'=>'真实姓名必须填写');
   			$validators['DCard'] = array(new Zend_Validate_NotEmpty()
   										   ,'messages'=>'身份证必须填写');
   			$validators['email'] = array(new Zend_Validate_NotEmpty()
   										,new Zend_Validate_EmailAddress()
   										,'messages'=>array(array(Zend_Validate_NotEmpty::IS_EMPTY => 'email必须填写.'),array(Zend_Validate_EmailAddress::INVALID_FORMAT=>'email格式不正确')));

    		$data = $this->getRequest()->getPost();
			$input = new Zend_Filter_Input(null, $validators, $data);
			
			/**
    		 * 验证数据正确性
    		 */
			if ($input->hasInvalid()){
				$this->_helper->viewRenderer->setNoRender();
				var_dump($input->getMessages());
				return ;
			}
			if($validators['password'] == $validators['confirmPassword']){
				$this->_helper->viewRenderer->setNoRender();
				echo '密码不一致';
				return ;
			}
			
			$userModel = new Application_Model_UserGuest();
			$data['ip'] = ip2long($_SERVER["REMOTE_ADDR"]);
			unset($data['confirmPassword']);
			if(!$userModel->guestReg($data)){
				$this->_helper->viewRenderer->setNoRender();
				echo 'ERROR';
				return ;
			}
    	}
    	//header("Set-Cookie: test=axis; domain=.my.hxpass; path=/");
    }

}

